package com.heshuo.basic.action;


import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;


import com.heshuo.basic.page.PageUtil;
import com.heshuo.oc.domain.UserLogin;
import com.heshuo.util.COMMON;
import com.heshuo.util.Constants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class BaseAction extends ActionSupport implements SessionAware,
ServletRequestAware, ServletResponseAware{
	
	protected SessionMap session;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected PageUtil pageUtil= new PageUtil();
	
	public void setSession(Map map) {
		this.session=(SessionMap) map;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response=response;
	}

	
	protected int getCurrentPage(){
		String currentPage="";
		try {
			currentPage=request.getParameter("page");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		if(COMMON.isEmpty(currentPage)){
			currentPage=1+"";
		}
		try {
			Integer.parseInt(currentPage);
		} catch (NumberFormatException e) {
			currentPage=1+"";
		}
		pageUtil.setCurrentPage(Integer.parseInt(currentPage));
		return Integer.parseInt(currentPage);
	}
	
	public String getUserName() throws Exception{
		ActionContext ctx = ActionContext.getContext();
		UserLogin user = (UserLogin)request.getSession().getAttribute(Constants.USERLOGIN_KEY);
		return user.getName();
	}
	public String getUserId() throws Exception{
		ActionContext ctx = ActionContext.getContext();
		UserLogin user = (UserLogin)request.getSession().getAttribute(Constants.USERLOGIN_KEY);
		return user.getUserLoginId();
	}

	public PageUtil getPageUtil() {
		return pageUtil;
	}

	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}
	
}
